package com.liuwei.assemble.vo;


import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;


public class TableFieldVO extends TableField {
    /**
     *{
     "showName":"显示列名",
     "type":"此值是数字型1:int,2:text,3:date,4:radio,5:checkbox,6:select",
     "typeValue":"当type为4,5,字符串数组 6:[{name:'',value:''}]",
     "showFlag":"boolean型，是否在前端显示"
     }
     是否显示^显示名^
     */
    private boolean flag = true;
    private ExtraFiledVO exterFiled;
    public ExtraFiledVO getExterFiled(){
        if(exterFiled==null && flag && StringUtils.isNotBlank(this.getComment())){
            flag = false;
            try{
                ObjectMapper objectMapper = new ObjectMapper();
                exterFiled = objectMapper.readValue(this.getComment(), ExtraFiledVO.class);
            }catch (Exception ex){
                //ex.printStackTrace();
                String [] strs = this.getComment().split("\\^");
                if(strs!=null && strs.length>1){
                    exterFiled = new ExtraFiledVO();
                    if("1".equals(strs[0])){
                        exterFiled.setShowFlag(true);
                    }
                    exterFiled.setShowName(strs[1]);
                    if(strs.length>2){
                        exterFiled.setType(strs[2]);
                    }
                }
            }
        }
        if(exterFiled==null){
            exterFiled = new ExtraFiledVO();
            exterFiled.setShowName(this.getPropertyName());
            exterFiled.setShowFlag(true);
            if(this.getColumnType()== DbColumnType.BOOLEAN){
                exterFiled.setType("select");
                Map t = new HashMap();
                t.put("name","是");
                t.put("value","1");
                Map f = new HashMap();
                f.put("name","否");
                f.put("value","0");
                List<Map> list = Arrays.asList(t,f);
                exterFiled.setTypeValues(list);
            }else if(this.getColumnType()== DbColumnType.DATE){
                exterFiled.setType("date");
            }else if(this.getColumnType()== DbColumnType.INTEGER || this.getColumnType()==DbColumnType.LONG){
                exterFiled.setType("int");
            }else{
                exterFiled.setType("text");
            }
        }
        return exterFiled;
    }
}
